package com.Spring.liaojp.controller;

import com.Spring.liaojp.config.DataBaseInitializer;
import com.Spring.liaojp.entity.Order;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;

/**
 * @author LiaoJianpeng
 * &#064;date  Created in 22:06 2019/12/26
 * &#064;description  订单控制器
 */
public class OrderController {
    private Set<Order> orders;

    /**
     * 获取订单集合
     * @return 订单集合
     */
    public Set<Order> getOrders() {
//        try {
//            Statement statement = DataBaseInitializer.connection.createStatement();
//            ResultSet resultSet = statement.executeQuery("select * from `order`");
//
//            while (resultSet.next()){
//                System.out.println("订单id：" + resultSet.getInt("order_id") + " 需求：" + resultSet.getInt("need") + " 价格：" + resultSet.getDouble("price"));
//            }
//
//            resultSet.close();
//            statement.close();
//            // todo connection关闭
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }

        return orders;
    }

    /**
     * 添加订单
     * @param order 订单
     */
    public boolean addOrders(Order order) {
        return this.orders.add(order);
    }

    /**
     * 通过id删除订单
     * @param id 订单id
     */
    public void removeOrderById(int id) {
        for (Order order : orders) {
            if (order.getId() == id) {
                orders.remove(order);
                break;
            }
        }
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }
}
